package com.fengmi.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 123
 * @Date 2022/1/25 20:26
 * @Version 1.0
 */

@Configuration
@EnableSwagger2
public class Swagger2Configuration {

    //@Bean
    //public Docket getDocket(){
    //    Docket docket = new Docket(DocumentationType.SWAGGER_2)
    //            .apiInfo(getApiInfo())  //获取Api文档首页的信息
    //            .select()  //初始化并返回一个api构造器
    //            .apis(RequestHandlerSelectors.any())  //为所有的方法创建API文档
    //            .paths(PathSelectors.any())  //可以根据url路径来指定为哪些接口生成api文档，哪些忽略
    //            .build();
    //
    //    return docket;
    //
    //}
    //
    ///**
    // * Api 首页信息设置方法
    // * @return
    // */
    //private ApiInfo getApiInfo() {
    //
    //    //作者信息
    //    Contact contact = new Contact("李晓冬","www.baidu.com","654744482@qq.com");
    //
    //    ApiInfo apiInfo = new ApiInfoBuilder()
    //            .title("《锋迷铺子接口文档》")   //接口文档标题
    //            .description("测试swagger2使用")  //接口文档描述
    //            .contact(contact) //作者信息
    //            .version("V1.0") //版本
    //            .build();
    //
    //    return apiInfo;
    //
    //}
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("锋迷铺子运营平台接口文档")
                .description("锋迷铺子运营平台接口文档")
                .version("1.0.0")
                .build();
    }
    @Bean
    public Docket createRestApi(Environment environment) {

        Profiles profiles = Profiles.of("dev");

        boolean enable = environment.acceptsProfiles(profiles);

        //添加head参数
        ParameterBuilder tokenPar = new ParameterBuilder();
        tokenPar.name("token").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();



        List<Parameter> pars = new ArrayList<Parameter>();
        pars.add(tokenPar.build());


        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(enable)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.qf")) //这里写的是API接口所在的包位置
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
    }

    private static List<ApiKey> unifiedAuth() {
        List<ApiKey> arrayList = new ArrayList();
        arrayList.add(new ApiKey("Authorization", "token", "header"));
        return arrayList;
    }

}